/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import dao.BilletDAO;
import dao.EvenementDAO;
import dao.KitDAO;
import dao.TransDAO;
import entity.Billet;
import entity.Evenement;
import entity.Kit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.jboss.weld.context.ejb.Ejb;

/**
 *
 * @author linos
 */
@ManagedBean(name = "kitConsultation")
@RequestScoped
public class KitConsultation {

    @Ejb
    private KitDAO kitDAO = new KitDAO();
    @Ejb
    private EvenementDAO evenementDAO = new EvenementDAO();
    @Ejb
    private BilletDAO billetDAO = new BilletDAO();
    private TransDAO transDAO = new TransDAO();
    List<Evenement> evenements = new ArrayList<Evenement>();
    Kit kit;
    Billet billet = new Billet();
    String ns;

    public KitConsultation() {
    }

    public List<Evenement> getEvenements() {
        return evenements;
    }

    public void setEvenements(List<Evenement> evenements) {
        this.evenements = evenements;
    }

    public Kit getKit() {
        return kit;
    }

    public void setKit(Kit kit) {
        this.kit = kit;
    }

    public Billet getBillet() {
        return billet;
    }

    public void setBillet(Billet billet) {
        this.billet = billet;
    }

    public void recherche() {
        ns = ns.replaceAll("=", "-");

        try {
            billet = (Billet) billetDAO.findByProperty("numero", ns).get(0);
            ns = "";
            if (!billet.getNumero().contains("KT")) {
                System.out.println("Non kit");
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Numero non Kit:" + ns, ""));
                billet = new Billet();
                ns = "";

            }
            try {
                System.out.println("idbillet=" + billet.getId());
                kit = (Kit) kitDAO.findByBillet(billet.getId()).get(0);
                System.out.println("kit spects" + kit.getIdsSpec());
                String[] s = kit.getIdsSpec().split(";");
                for (int i = 0; i < s.length; i++) {
                    evenements.add(evenementDAO.findById(Integer.parseInt(s[i])));
                }
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "KIT:" + kit.getId(), ""));

            } catch (IndexOutOfBoundsException e) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Kit non vendu:" + ns, ""));

            }

        } catch (IndexOutOfBoundsException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Numero Inexistant:" + ns, ""));

        }

    }

    public String getNs() {
        return ns;
    }

    public void setNs(String ns) {
        this.ns = ns;
    }

    public String utiliser() {
        if (billet.getUtilise()) {
            return "Utilisée";
        }
        return "Non utilisée";
    }

    public String spec() {
        Date d = new Date();
        d.setHours(d.getHours());
        System.out.println("Date=" + d);
        try{
        return ((Evenement) evenementDAO.findByProperty("date", d).get(0)).getTitre();
        }
        catch(IndexOutOfBoundsException e)
        {
            return "Aucun spectacle";
        }
    }

    public String include() {
        Date d = new Date();
        
        try {
            System.out.println("Date=" + d);
            System.out.println("Date event " + billet.getTypeb().getEvenement().getDate());
            if (billet.getTypeb().getEvenement().getDate().getDay() == d.getDay()) {
                return "Inclu";
            }
            return "Non Inclu";

        } catch (Exception e) {
            return "cc";
        }

    }
    
    
    public String blod(String s)
    {
       // System.out.println("fasda "+billet.getTypeb().getTypeBillet().getEvenement().getTitre());
        if(s.equals(spec()))
            return "font-weight: bold;color: red;";
        return "";
    }
}